package com.ruoyi.edu.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.edu.api.domain.EduCourse;
import com.ruoyi.edu.api.domain.EduTeacher;
import com.ruoyi.edu.api.domain.vo.EduCommentVO;
import com.ruoyi.edu.mapper.EduCourseMapper;
import com.ruoyi.edu.mapper.EduTeacherMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.mapper.EduCommentMapper;
import com.ruoyi.edu.api.domain.EduComment;
import com.ruoyi.edu.service.IEduCommentService;

/**
 * 评论Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements IEduCommentService {
    @Resource
    private EduCommentMapper eduCommentMapper;
    @Resource
    private EduCourseMapper eduCourseMapper;
    @Resource
    private EduTeacherMapper eduTeacherMapper;

    /**
     * 查询评论
     *
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public EduComment selectEduCommentById(String id) {
        return eduCommentMapper.selectEduCommentById(id);
    }

    /**
     * 查询评论列表
     *
     * @param eduComment 评论
     * @return 评论
     */
    @Override
    public List<EduCommentVO> selectEduCommentList(EduComment eduComment) {
        // 获得符合查询条件的原本数据
        List<EduComment> eduComments = eduCommentMapper.selectEduCommentList(eduComment);
        // 如果符合条件的评论为空，直接返回null
        if (eduComments == null || eduComments.size() == 0) {
            return null;
        }
        // 准备要返回给前端的列表
        List<EduCommentVO> result = new ArrayList<>(eduComments.size());
        /* 封装需要的课程名称和讲师名称 */
        // 1、获取课程名称并转为map结构
        Map<String, String> courseMap = eduCourseMapper.selectList(null).stream().collect(Collectors.toMap(EduCourse::getId, EduCourse::getTitle));
        // 2、获取讲师名称并转为map结构
        Map<String, String> teacherMap = eduTeacherMapper.selectList(null).stream().collect(Collectors.toMap(EduTeacher::getId, EduTeacher::getName));
        // 3、封装结果
        for (EduComment comment : eduComments) {
            EduCommentVO eduCommentVO = new EduCommentVO();
            BeanUtils.copyProperties(comment, eduCommentVO);
            eduCommentVO.setCourseName(courseMap.get(eduCommentVO.getCourseId()));
            eduCommentVO.setTeacherName(teacherMap.get(eduCommentVO.getTeacherId()));
            // 把赋值好的对象放进结果列表
            result.add(eduCommentVO);
        }
        return result;
    }

    /**
     * 新增评论
     *
     * @param eduComment 评论
     * @return 结果
     */
    @Override
    public int insertEduComment(EduComment eduComment) {
        return eduCommentMapper.insertEduComment(eduComment);
    }

    /**
     * 修改评论
     *
     * @param eduComment 评论
     * @return 结果
     */
    @Override
    public int updateEduComment(EduComment eduComment) {
        return eduCommentMapper.updateEduComment(eduComment);
    }

    /**
     * 批量删除评论
     *
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteEduCommentByIds(String[] ids) {
        return eduCommentMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除评论信息
     *
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteEduCommentById(String id) {
        return eduCommentMapper.deleteById(id);
    }
}
